//
//  KFDNotificationSettings.m
//  KFD
//
//  Created by Lucky Luke on 10/11/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDMessageNotificationSettings.h"

#define kMessageNotificationSettings @"KFDMessageNotificationSettings"

#define kMasterSwitch @"KFDMessageNotificationSettingsMasterSwitch"
#define kSound @"KFDMessageNotificationSettingsSound"
#define kVibration @"KFDMessageNotificationSettingsVibration"
#define kContent @"KFDMessageNotificationSettingsContent"
#define kFlash @"KFDMessageNotificationSettingsFlash"

@implementation KFDMessageNotificationSettings

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
        self.masterSwitch=((NSNumber*)[aDecoder decodeObjectForKey:kMasterSwitch]).boolValue;
        self.sound=((NSNumber*)[aDecoder decodeObjectForKey:kSound]).boolValue;
        self.vibration=((NSNumber*)[aDecoder decodeObjectForKey:kVibration]).boolValue;
        self.content=((NSNumber*)[aDecoder decodeObjectForKey:kContent]).boolValue;
        self.flash=((NSNumber*)[aDecoder decodeObjectForKey:kFlash]).boolValue;
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:@(self.masterSwitch) forKey:kMasterSwitch];
    [aCoder encodeObject:@(self.sound) forKey:kSound];
    [aCoder encodeObject:@(self.vibration) forKey:kVibration];
    [aCoder encodeObject:@(self.content) forKey:kContent];
    [aCoder encodeObject:@(self.flash) forKey:kFlash];
}

-(void)saveToUserDefaults
{
    NSMutableDictionary* dictionary=[NSMutableDictionary dictionary];
    [dictionary setObject:@(self.masterSwitch) forKey:kMasterSwitch];
    [dictionary setObject:@(self.sound) forKey:kSound];
    [dictionary setObject:@(self.vibration) forKey:kVibration];
    [dictionary setObject:@(self.content) forKey:kContent];
    [dictionary setObject:@(self.flash) forKey:kFlash];
    NSUserDefaults* userDefaults=[NSUserDefaults standardUserDefaults];
    [userDefaults setObject:dictionary forKey:kMessageNotificationSettings];
}

+(KFDMessageNotificationSettings*)loadFromUserDefaults
{
    NSUserDefaults* userDefaults=[NSUserDefaults standardUserDefaults];
    NSDictionary* dictionary=[userDefaults objectForKey:kMessageNotificationSettings];
    if (dictionary) {
        KFDMessageNotificationSettings* settings=[KFDMessageNotificationSettings new];
        settings.masterSwitch=((NSNumber*)[dictionary objectForKey:kMasterSwitch]).boolValue;
        settings.sound=((NSNumber*)[dictionary objectForKey:kSound]).boolValue;
        settings.vibration=((NSNumber*)[dictionary objectForKey:kVibration]).boolValue;
        settings.content=((NSNumber*)[dictionary objectForKey:kContent]).boolValue;
        settings.flash=((NSNumber*)[dictionary objectForKey:kFlash]).boolValue;
        return settings;
    }
    return nil;
}

@end
